/* Bar at the bottom of the UI holding the 4 stance buttons
 * 
 */

package gui.battle;

import item.ship.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import gui.utilities.*;

import javax.swing.*;

public class StanceBar extends JPanel{

	ArrayList<MySelectableButton> buttons;
	private Ship ship;

	//1 param constructor that sets up the stance bar
	public StanceBar(Ship s){

		this.setMaximumSize(new Dimension(300, 60));
		this.setLayout(new GridLayout(1, 4));

		String normalButtonPath = "assets\\images\\buttonImages\\normal\\";
		String hoverButtonPath = "assets\\images\\buttonImages\\hover\\";
		String selectButtonPath = "assets\\images\\buttonImages\\select\\";
		ship = s;

		ArrayList<String> buttonNames = new ArrayList<String>(4);
		buttonNames.add("normal_stance.png");
		buttonNames.add("offensive_stance.png");
		buttonNames.add("defensive_stance.png");
		buttonNames.add("evasive_stance.png");

		buttons = new ArrayList<MySelectableButton>(4);

		//if the panel is for the currently selected ship
		if(s == BattlePanel.turn.getShip()){
			for(int i = 0; i < 4; i++){

				buttons.add(new MySelectableButton(normalButtonPath + buttonNames.get(i), hoverButtonPath + buttonNames.get(i), selectButtonPath + buttonNames.get(i), 75, 60));
				
				buttons.get(i).addMouseListener(new MouseAdapter(){

					//when the button is clicked
					public void mouseClicked(MouseEvent e){

						if(BattlePanel.turn.getShip().getActionPoints() > 1){

							if(!((MySelectableButton)e.getComponent()).getSelected()){
								for(MySelectableButton b: buttons){

									b.deselectButton();
								}//for

								((MySelectableButton) e.getComponent()).selectButton();

								int stance = buttons.indexOf(e.getComponent());



								switch(stance){

								case 0:
									ship.setStance(Stance.NORMAL);
									break;

								case 1:
									ship.setStance(Stance.OFFENSIVE);
									break;

								case 2:
									ship.setStance(Stance.DEFENSIVE);
									break;

								case 3:
									ship.setStance(Stance.EVASIVE);							
								}//switch

								if(BattlePanel.turn.getShip().getActionPoints() == 5)
									BattlePanel.turn.getShip().setActionPoints(4);

								else
									BattlePanel.turn.getShip().setActionPoints(BattlePanel.turn.getShip().getActionPoints() - 2);

								BattlePanel.sip.updateActionPoints();
							}//if
						}//if
					}//mouseClicked
				});
				
				this.add(buttons.get(i));
			}//for	
			
			int stance = ship.getStance().ordinal();
			
			switch(stance){
			
			case 0:
				buttons.get(0).selectButton();
				break;
			
			case 1:
				buttons.get(1).selectButton();
				break;
			
			case 2:
				buttons.get(2).selectButton();
				break;
				
			case 3:
				buttons.get(3).selectButton();
				break;
			}//switch
		}//if

		else{

			ArrayList<JLabel> icons = new ArrayList<JLabel>(4); //using icons instead of buttons as they do not change

			for(int i = 0; i < 4; i++)
				icons.add(new JLabel(new ImageIcon(normalButtonPath + buttonNames.get(i))));

			int stance = ship.getStance().ordinal();

			icons.set(stance, new JLabel(new ImageIcon(selectButtonPath + buttonNames.get(stance))));

			for(JLabel l: icons)
				this.add(l);
		}//else

	}//Ship constructor
}//StanceBar
